package com.miplus.ccrm.core.service.strategy;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.strategy.*;
import com.miplus.ccrm.core.mapper.CfgActivityExecdayMapper;
import com.miplus.ccrm.core.pojo.*;
import com.miplus.ccrm.core.mapper.CfgActivityMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author pc
* @description 针对表【cfg_activity(自动分案管理表)】的数据库操作Service实现
* @createDate 2024-03-22 17:43:40
*/
@Service
public class CfgActivityServiceImpl extends MPJBaseServiceImpl<CfgActivityMapper, CfgActivity>
    implements CfgActivityService{
    @Resource
    private CfgActivityOthergrplinkService cfgActivityOthergrplinkService;
    @Resource
    private CfgActivityQuerylinkService cfgActivityQuerylinkService;
    @Resource
    private CfgActivityExecdayMapper cfgActivityExecdayMapper;

    @Override
    public IPage<ActivityResVo> cfgActivityList(ActivityReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                ActivityResVo.class,
                new MPJLambdaWrapper<CfgActivity>()
                        .selectAs("fun_getnames('BUSINESS.TYPE',t.businesstype)",ActivityResVo::getBusinesstypedsc)
                        .selectAs("fun_getnames('ACTIVITY.QUERY.DISTYPE',t.distype)",ActivityResVo::getDistypedsc)
                        .selectAs("t.overday1||'-'||t.overday2",ActivityResVo::getOverday)
                        .selectAs("t.availableday1||'~'||t.availableday2",ActivityResVo::getAvailableday)
                        .selectAs("(select tg.name from cp_calllist_trigger tg where tg.triggerid=t.triggerid)",ActivityResVo::getTriggername)
                        .selectAll(CfgActivity.class)
                        .like(JPubUtils.IsNotEmpty(paras.getName()),"name",paras.getName())
                        .eq(paras.getStatus()!=null,"status",paras.getStatus())
                        .orderByAsc("t.rank")
        );
    }

    @Override
    public CmdResVo cfgActivityUpdate(String $usrid, ActivityEditVo paras)
    {
        //2024-08-05
        int ret=baseMapper.update(null,new UpdateWrapper<CfgActivity>()
                .lambda()
                .set(CfgActivity::getName,paras.getName())
                .set(CfgActivity::getRemark,paras.getRemark())
                .set(CfgActivity::getDistype,paras.getDistype())
                .set(CfgActivity::getDistype2,paras.getDistype2())
                .set(CfgActivity::getOthergrp,paras.getOthergrp())
                .set(CfgActivity::getAvailableday1,JPubUtils.StringToDate(paras.getAvailableday1()))
                .set(CfgActivity::getAvailableday2,JPubUtils.StringToDate(paras.getAvailableday2()))
                .set(CfgActivity::getCity,paras.getCity())
                .set(CfgActivity::getBusinesstype,paras.getBusinesstype())
                .set(CfgActivity::getMthsOdue,paras.getMthsOdue())
                .set(CfgActivity::getMthsOdue2,paras.getMthsOdue2())
                .set(CfgActivity::getOverday1,paras.getOverday1())
                .set(CfgActivity::getOverday2,paras.getOverday2())
                .set(CfgActivity::getGrpid,paras.getGrpid())
                .set(CfgActivity::getCusttags,paras.getCusttags())
                .set(CfgActivity::getNoinctag,paras.getNoinctag())
                .set(CfgActivity::getBindday,paras.getBindday())
                .set(CfgActivity::getDistdaymode,paras.getDistdaymode())
                .set(CfgActivity::getAcctFlevel,paras.getAcctFlevel())
                .set(CfgActivity::getTriggerid,paras.getTriggerid())
                .set(CfgActivity::getMddt,new Date())
                .set(CfgActivity::getMdusr,$usrid)
                .set(CfgActivity::getStatus,paras.getStatus())
                .eq(CfgActivity::getActivityid,paras.getActivityid())
        );

        cfgActivityOthergrplinkService.getBaseMapper().delete(
                new MPJLambdaWrapper<CfgActivityOthergrplink>().eq("activityid",paras.getActivityid())
        );
        cfgActivityExecdayMapper.delete(
                new MPJLambdaWrapper<CfgActivityExecday>().eq("activityid",paras.getActivityid())
        );
        updateLinkTable(paras.getActivityid(),paras);

        return new CmdResVo(ret,paras.getActivityid().toString());
    }

    @Override
    public CmdResVo cfgActivityInsert(String $usrid,ActivityEditVo paras)
    {
        CfgActivity def=new CfgActivity();

        BeanUtils.copyProperties(paras,def);
        int max=getActivityMaxRank();
        //def.setRank(max);
        def.setCrdt(new Date());
        def.setCrusr($usrid);
        def.setMddt(new Date());
        def.setMdusr($usrid);
        int ret=baseMapper.insert(def);

        //for TDSQL
        baseMapper.updateRank(def.getActivityid(),max);

        updateLinkTable(def.getActivityid(),paras);
        return new CmdResVo(ret,def.getActivityid().toString());
    }

    private int getActivityMaxRank()
    {
        Long num=baseMapper.selectCount(new QueryWrapper<>());
        if(num>0) {
            List<Integer> max = baseMapper.getMaxRank();
            if (JPubUtils.IsEmpty(max))
                return 1;
            return max.get(0) + 1;
        }
        else return 1;
    }

    private void updateLinkTable(Integer activityid,ActivityEditVo paras)
    {
        if(JPubUtils.IsNotEmpty(paras.getOutgrplist())){
            for(ActGrplinkVo g : paras.getOutgrplist()){
                CfgActivityOthergrplink l=new CfgActivityOthergrplink();
                l.setActivityid(activityid);
                l.setGrpid(g.getGrpid());
                l.setRadio(g.getRadio());
                cfgActivityOthergrplinkService.getBaseMapper().insert(l);
            }
        }
        if(JPubUtils.IsNotEmpty(paras.getExecdaylist())){
            for(ActExecDayVo d : paras.getExecdaylist()){
                CfgActivityExecday f=new CfgActivityExecday();
                f.setActivityid(activityid);
                Date day=JPubUtils.StringToDate(d.getDistDTime());
                if(day!=null) {
                    f.setExecdate(JPubUtils.StringToDate(d.getDistDTime()));
                    f.setRecydate(JPubUtils.StringToDate(d.getRecyDTime()));
                    cfgActivityExecdayMapper.insert(f);
                }
            }
        }
    }

    @Override
    public CmdResVo cfgActivityDelete(int id)
    {
        int ret=baseMapper.deleteById(id);
        cfgActivityOthergrplinkService.getBaseMapper().delete(
                new QueryWrapper<CfgActivityOthergrplink>().eq("activityid",id)
        );
        cfgActivityQuerylinkService.getBaseMapper().delete(
                new QueryWrapper<CfgActivityQuerylink>().eq("activityid",id)
        );
        cfgActivityExecdayMapper.delete(
                new QueryWrapper<CfgActivityExecday>().eq("activityid",id)
        );
        return new CmdResVo(ret,Integer.toString(id));
    }

    @Override
    public CmdResVo upCfgActivity(int activityid)
    {
        //FOR TDSQL
        //CfgActivity f=baseMapper.selectById(activityid);
        CfgActivity f=baseMapper.activitySelectOne(activityid);
        Integer rank=f.getRank();
        if(rank==null||rank<=1){
            return new CmdResVo(0,Integer.toString(activityid));
        }
        //FOR TDSQL
        //rank=rank-1;
        Integer rankold=rank-1;
        //CfgActivity def=new CfgActivity();
        //def.setRank(rank+1);
        //int ret=baseMapper.update(def,
         //           new QueryWrapper<CfgActivity>()
         //           .eq("t.rank",rank)
       // );
        int ret=baseMapper.updateRankAsc(rankold,rank+1);
        //def=new CfgActivity();
        //def.setActivityid(activityid);
        //def.setRank(rank);
        //baseMapper.updateById(def);
        baseMapper.updateRank(activityid,rank);
        return new CmdResVo(ret,Integer.toString(activityid));
    }

    @Override
    public CmdResVo downCfgActivity(int activityid)
    {
        //for TDSQL
        //CfgActivity f=baseMapper.selectById(activityid);
        CfgActivity f=baseMapper.activitySelectOne(activityid);
        Integer rank=f.getRank();
        List<Integer> max=baseMapper.getMaxRank();
        Integer maxrank=max.get(0);
        if(rank==null||rank>=maxrank){
            return new CmdResVo(0,Integer.toString(activityid));
        }
        Integer rankold=rank+1;
        //for tdsql
        //CfgActivity def=new CfgActivity();
        //def.setRank(rank-1);
        //int ret=baseMapper.update(def,
         //       new QueryWrapper<CfgActivity>()
         //               .eq("t.rank",rank)
        //);
        int ret=baseMapper.updateRankAsc(rankold,rank-1);
        //def=new CfgActivity();
        //def.setActivityid(activityid);
        //def.setRank(rank);
        //baseMapper.updateById(def);
        baseMapper.updateRank(activityid,rank);
        return new CmdResVo(ret,Integer.toString(activityid));
    }

    @Override
    public IPage<ActOGrpLinkResVo> activityOutGrpList(ActOGrpLinkReqVo paras)
    {
        return cfgActivityOthergrplinkService.activityOutGrpList(paras);
    }

    @Override
    public List<ActExecDayVo> getActivityExecDay(Integer activityid)
    {
        List<CfgActivityExecday> dateList=cfgActivityExecdayMapper.selectList(new QueryWrapper<CfgActivityExecday>()
                    .eq("activityid",activityid)
                    .select("execdate","recydate")
                    .orderByAsc("id")
        );
        List<ActExecDayVo> list=new ArrayList<>();
        for(CfgActivityExecday g : dateList){
            ActExecDayVo vo=new ActExecDayVo();
            vo.setDistDTime(JPubUtils.DateToString(g.getExecdate()));
            vo.setRecyDTime(JPubUtils.DateToString(g.getRecydate()));
            list.add(vo);
        }
        return list;
    }


}




